class Solution 
{
public:
    int sq_sum(int n)
    {   
        int sum = 0;
        while(n)
        {
            int tmp = n % 10;
            sum += tmp * tmp;
            n /= 10;
        }

        return sum;
    }

    bool isHappy(int n) 
    {
        int slow = n, fast = sq_sum(n);
        while(slow != fast)
        {
            slow = sq_sum(slow);
            fast = sq_sum(sq_sum(fast));
        }

        return fast == 1;  // 相遇时的值是否为1
    }
};